package com.okcl.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.okcl.common.exception.BusinessException;
import com.okcl.manager.mapper.SysRoleMapper;
import com.okcl.manager.mapper.SysRoleMenuMapper;
import com.okcl.manager.service.SysMenuService;
import com.okcl.manager.service.SysRoleService;
import com.okcl.model.dto.system.AssginMenuDto;
import com.okcl.model.dto.system.SysRoleDto;
import com.okcl.model.entity.system.SysMenu;
import com.okcl.model.entity.system.SysRole;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 查询角色列表
     *
     * @param sysRoleDto
     * @return
     */
    @Override
    public PageInfo<SysRole> queryPageRoleList(SysRoleDto sysRoleDto) {
        Integer currentPage = sysRoleDto.getPageNum();
        Integer pageSize = sysRoleDto.getPageSize();
        //设置分页参数
        PageHelper.startPage(currentPage, pageSize);
        List<SysRole> sysRoleList = sysRoleMapper.selectPageRoleList(sysRoleDto);
        return new PageInfo<>(sysRoleList);
    }

    /**
     * 添加角色
     *
     * @param sysRole
     */
    @Override
    public void save(SysRole sysRole) {
        sysRole.setCreateTime(LocalDateTime.now());
        sysRoleMapper.save(sysRole);
    }

    /**
     * 修改角色
     *
     * @param sysRole
     */
    @Transactional
    @Override
    public void edit(SysRole sysRole) {
        sysRole.setUpdateTime(LocalDateTime.now());
        int edit = sysRoleMapper.edit(sysRole);
        if (edit <= 0) {
            throw new BusinessException(500, "修改失败");
        }
    }

    /**
     * 删除角色
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        int i = sysRoleMapper.deleteByLogical(id);
        if (i <= 0) {
            throw new BusinessException(500, "删除失败");
        }
    }

    /**
     * 为角色分配菜单
     *
     * @param assginMenuDto
     */
    @Override
    @Transactional
    public void assignMenu(AssginMenuDto assginMenuDto) {
        Long roleId = assginMenuDto.getRoleId();
        //先删除之前的角色菜单关系数据
        sysRoleMenuMapper.deleteLogicalByRoleId(roleId);
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (CollectionUtils.isEmpty(menuIdList) && menuIdList.isEmpty()) {
            return;
        }
        //保存角色菜单数据
        sysRoleMenuMapper.save(roleId, assginMenuDto.getMenuIdList());
    }

    /**
     * 查询角色已分配的菜单
     * 查询所有菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> queryAssignedMenu(Long roleId) {
        List<Long> menuIdList = sysRoleMenuMapper.selectMenuIdListByRoleId(roleId);
        List<SysMenu> sysMenuList = sysMenuService.selectMenuList();
        Map<String, Object> map = new HashMap<>();
        map.put("menuTreeList", sysMenuList);//所有菜单数据
        map.put("checkedKeys", menuIdList);//已经分配的菜单数据
        return map;
    }
}
